﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using JWFW.SFL;
using JWFW.Encrypt;
using JWFW.BOL;

namespace JWFW.Model.Register
{
    //随机加密的次数在验证码首位定义
    //试用版
    //验证码只在服务端生成
    //工具代号只在服务端中有，验证码中包含：机器码，客户ID，工具ID，
    //客户端只提供机器码即可

    /// <summary>
    /// 注册信息类。
    /// 使用方法：
    /// 1.新输入注册时：由CreateName()获取注册用的Name，调用Save()保存，随后调用Init()，再调用检查函数，弹出成功与否信息
    /// 2.注册事件
    /// 3.检查账户数：在需要添加，加载账户的地方调用
    /// 4.调用普通校验来检查注册的合法性
    /// 5.运行时设置Running
    /// 6.异常时终止运行，设置Running
    /// 7.
    /// </summary>
    public class RegInfo
    {
        /// <summary>
        /// 静态函数，获取机器码
        /// </summary>
        static RegInfo()
        {
            s_mc = GetMC();
        }

        /// <summary>
        /// 访问WS的代理类
        /// </summary>
        protected static IValidateReg s_VR = null;
        /// <summary>
        /// 设置WS的URL
        /// </summary>
        /// <param name="vr">WS代理</param>
        /// <param name="url">WS的url</param>
        public static void SetWSURL(IValidateReg vr, string url)
        {
            s_VR = vr;
            s_VR.Url = url;
        }

        private RegInfo()
        {
        }
        /// <summary>
        /// 默认构造函数
        /// </summary>
        /// <param name="toolCode">工具代号</param>
        /// <param name="isPC">是否是单机版</param>
        public RegInfo(string toolCode, bool isPC)
        {
            m_toolCode = toolCode;
            m_isPC = isPC;
            this.Init();
            this.InitTimer();
            this.Start();
        }
        /// <summary>
        /// 指定配置文件的构造函数
        /// </summary>
        /// <param name="regFileName">配置文件</param>
        /// <param name="toolCode">工具代号</param>
        /// <param name="isPC">是否是单机版</param>
        public RegInfo(string regFileName, string toolCode, bool isPC)
        {
            m_regFileName = regFileName;
            m_toolCode = toolCode;
            m_isPC = isPC;
            this.Init();
            this.InitTimer();
            this.Start();
        }

        //网络验证:在Timer中添加getinfo的调用
        //msg使用b64加密再解密显示出来

        #region 机器码

        /// <summary>
        /// 机器码
        /// </summary>
        protected static string s_mc = "";
        /// <summary>
        /// 获取机器码：
        /// 流程：
        /// 1.获取硬盘序列号
        /// 2.获取MAC
        /// 3.硬盘序列号的MD5加密从前，MAC的MD5加密从后逐个字符叠加，并映射到A-Z之间
        /// 字典中备用的硬盘序列号以及MAC用于其他地方进行验证
        /// </summary>
        /// <returns>客户电脑的机器码</returns>
        protected static string GetMC()
        {
            return CombineStrings(EnvironmentOP.GetHDInfo(0), EnvironmentOP.GetMacAddress().Replace(":", ""));
        }
        /// <summary>
        /// 补足字符串长度
        /// </summary>
        /// <param name="origin">待补足长度的原始字符串</param>
        /// <param name="length">需要补足到的长度</param>
        /// <returns>补足后的字符串</returns>
        protected static string FillString(string origin, int length)
        {
            string table = "DA12B4C35EF6";
            int delta = length - origin.Length;
            if (delta < 0)
            {
                return origin.Substring(0, length);
            }
            for (int i = 0; i < delta; i++)
            {
                int tmp = CommonOP.GetStringSum(origin, 0, origin.Length);
                tmp = tmp * 7 % table.Length;
                origin = origin + table[tmp].ToString();
            }
            return origin;
        }
        /// <summary>
        /// 交叉组合两个字符串
        /// </summary>
        /// <param name="str1">字符串1</param>
        /// <param name="str2">字符串2</param>
        /// <returns>交叉运算后的字符串</returns>
        protected static string CombineStrings(string str1, string str2)
        {
            string res = "";
            str1 = MD5.GetMD5_16(str1);// FillString(str1, 12);
            str2 = MD5.GetMD5_16(str2);// FillString(str2, 12);
            for (int i = 0, j = str1.Length - 1; i < str1.Length; i++, j--)
            {
                int tmp = char.ConvertToUtf32(str1, i) + char.ConvertToUtf32(str2, j);
                tmp = tmp % 26 + 65;
                res = res + char.ConvertFromUtf32(tmp);
            }
            return res;
        }

        #endregion

        #region 事件

        /// <summary>
        /// 抛出Timer中出现的异常
        /// </summary>
        public delegate void ThrowTimerExceptionHandler(Exception ex);
        /// <summary>
        /// 抛出Timer中出现的异常
        /// </summary>
        public static event ThrowTimerExceptionHandler OnThrowTimerException;
        protected void DoThrowTimerException(Exception ex)
        {
            if (OnThrowTimerException != null)
            {
                OnThrowTimerException(ex);
            }
        }

        /// <summary>
        /// 帐户数检查
        /// </summary>
        public delegate void NumberCheckHandler();
        /// <summary>
        /// 帐户数检查
        /// </summary>
        public event NumberCheckHandler OnNumberCheck;
        protected void DoNumberCheck()
        {
            if (OnNumberCheck != null)
            {
                OnNumberCheck();
            }
        }

        /// <summary>
        /// 验证Name失败
        /// </summary>
        public delegate void CheckFailHandler();
        /// <summary>
        /// 验证Name失败
        /// </summary>
        public event CheckFailHandler OnCheckFail;
        protected void DoCheckFail()
        {
            if (OnCheckFail != null)
            {
                OnCheckFail();
            }
        }

        #endregion

        #region 变量

        protected string m_regFileName = "Config.dat";
        protected System.Timers.Timer m_timer = new System.Timers.Timer();
        protected Random m_random = new Random((int)DateTime.Now.Ticks);
        protected int m_timerInterval = 10;
        /// <summary>
        /// 用于保存数据的字典
        /// </summary>
        protected Dictionary<string, string> m_hash = new Dictionary<string, string>();
        protected bool m_running = false;


        protected string m_name = "";
        protected string m_regCode = "";
        protected string m_toolCode = "";
        protected bool m_isPC = true;
        protected DateTime m_date = DateTime.MinValue;
        protected int m_number = 1;
        protected string m_reflectorInfo = "";

        /// <summary>
        /// 网络验证需要额外的运行信息
        /// </summary>
        protected string m_netInfo = "";

        #endregion

        #region 属性

        /// <summary>
        /// 获取保存注册信息的配置文件
        /// </summary>
        public string RegFileName
        {
            get { return m_regFileName; }
        }
        /// <summary>
        /// 获取或设置程序是否处于运行中，确定是否必须进行注册信息检查
        /// </summary>
        public bool Running
        {
            get { return m_running; }
            set { m_running = value; }
        }
        /// <summary>
        /// 获取注册使用的Name：用户名或机器码与ToolCode组合
        /// </summary>
        public string Name
        {
            get { return m_name; }
        }
        /// <summary>
        /// 获取注册码
        /// </summary>
        public string RegCode
        {
            get { return m_regCode; }
        }
        /// <summary>
        /// 获取工具代号
        /// </summary>
        public string ToolCode
        {
            get { return m_toolCode; }
        }
        /// <summary>
        /// 获取工具到期时间
        /// </summary>
        public DateTime Date
        {
            get { return m_date; }
        }

        /// <summary>
        /// 获取反射信息
        /// </summary>
        protected string ReflectorInfo
        {
            get { return m_reflectorInfo; }
        }

        /// <summary>
        /// 获取备用的加密后的注册码
        /// </summary>
        public string BakCode
        {
            get
            {
                return this.GetBakCode();
            }
        }
        /// <summary>
        /// 获取本机机器码
        /// </summary>
        public string MC
        {
            get { return s_mc; }
        }

        #endregion

        #region Timer操作

        /// <summary>
        /// Timer启动，将在运行过程中检查注册信息
        /// </summary>
        protected void Start()
        {
            if (!m_timer.Enabled)
            {
                m_timer.Start();
            }
        }
        /// <summary>
        /// 初始化Timer
        /// </summary>
        protected void InitTimer()
        {
            m_timer.Elapsed -= new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
            m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
            m_timer.Interval = 1000 * m_timerInterval;
        }

        protected void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.DoTimer();
        }

        /// <summary>
        /// Timer触发后的执行，若有任何异常将会退出程序
        /// </summary>
        protected virtual void DoTimer()
        {
            try
            {
                //运行时必须检查暗桩
                if (m_running)
                {
                    this.ValidateSnag();
                }
                else
                {
                    //非运行时无注册信息，不需要检查暗桩
                    if (this.Name == "" || this.RegCode == "")
                    {
                        return;
                    }
                    //非运行时，注册信息是正确的，需要检查暗桩
                    if (this.CheckName() && this.CheckToolCode())
                    {
                        this.ValidateSnag();
                    }
                    //非运行时，注册信息是错误的，不需要检查暗桩，但是交由主程序处理
                    else
                    {
                        DoCheckFail();//建议主程序可以随机的报告注册信息不正确，并停止运行，防止破解设置了Running标志
                    }
                }
            }
            catch (Exception ex)
            {
                DoThrowTimerException(ex);//建议主程序任何异常都将随机报出，并停止运行
            }
        }

        #endregion

        /// <summary>
        /// 从配置文件中取出注册码并加密返回备用，用于暗桩验证中
        /// </summary>
        /// <returns></returns>
        protected string GetBakCode()
        {
            string res = "";
            lock (this)
            {
                if (File.Exists(m_regFileName))
                {
                    res = File.ReadAllText(m_regFileName);
                    res = EncryptOP.DecryptString(res);
                    string[] infos = CommonOP.SplitString(res, CommonOP.Spliter);
                    if (infos.Length == 2)
                    {
                        res = EncryptOP.EncryptString(infos[1]);
                    }
                }
            }
            return res;
        }
        /// <summary>
        /// 随机发生器，按指定概率随机执行操作
        /// </summary>
        /// <param name="rate"></param>
        /// <returns></returns>
        protected bool RandomAction(int rate)
        {
            return m_random.Next(100) < rate;
        }
        /// <summary>
        /// 重置各信息
        /// </summary>
        protected void ResetInfo()
        {
            m_name = "";
            m_regCode = "";
            m_date = DateTime.MinValue;
            m_number = 1;
            m_reflectorInfo = "";
        }
        /// <summary>
        /// 暗桩验证
        /// </summary>
        protected void ValidateSnag()
        {
            if (RandomAction(10))
            {
                this.Init();
            }
            if (RandomAction(10))
            {
                VNM();
            }
            if (RandomAction(5))
            {
                if (!this.CheckDate(true))
                {
                    throw new JWFW.BOL.BaseCustomException(JWFW.BOL.LanguageResource.GetInfo(LanguageResource.EX_Model_Register_ExpiredErr));
                }
                if (RandomAction(30))//降低验证几率，提高效率
                {
                    VDT();
                }
            }
            if (RandomAction(10))
            {
                VRC();
            }
            if (RandomAction(10))
            {
                VTC();
            }
            if (RandomAction(10))
            {
                VCRC();
            }
            if (RandomAction(50))
            {
                DoNumberCheck();
            }
            if (!m_isPC && RandomAction(10))
            {
                m_reflectorInfo = "";
            }
        }
        /// <summary>
        /// 移除CRC校验码
        /// </summary>
        /// <param name="origin"></param>
        /// <returns></returns>
        protected string RemoveCRC(string origin)
        {
            string res = EncryptOP.DecryptString(m_regCode).Remove(0, 2);
            res = res.Remove(res.Length - 2, 2);
            return res;
        }
        /// <summary>
        /// 从账户数加密串中取出真正的账户数字
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        protected string GetNumber(string number)
        {
            string res = EncryptOP.DecryptString(number).Remove(0, 2);
            res = res.Remove(res.Length - 2, 2);
            return res;
        }
        /// <summary>
        /// 验证1位CRC
        /// </summary>
        /// <param name="origin">待验证字符串</param>
        /// <param name="head">CRC在头部还是尾部</param>
        /// <returns></returns>
        protected bool CheckCRC(string origin, bool head)
        {
            int tmp = 0;
            if (origin.Length < 1)
            {
                return false;
            }
            if (head)
            {
                for (int i = 1; i < origin.Length; i++)
                {
                    tmp += char.ConvertToUtf32(origin, i);
                }
                return char.ConvertFromUtf32(tmp % 26 + 65) == origin[0].ToString();
            }
            else
            {
                for (int i = 0; i < origin.Length - 1; i++)
                {
                    tmp += char.ConvertToUtf32(origin, i);
                }
                return char.ConvertFromUtf32(tmp % 26 + 65) == origin[origin.Length - 1].ToString();
            }
        }

        /// <summary>
        /// 关闭工具
        /// </summary>
        public void Close()
        {
            System.Environment.Exit(0);
            ThrowRandom();//若退出代码被破解，则随机抛出异常
        }
        /// <summary>
        /// 创建Name【单机版：由机器码与ToolCode组成；网络版：原样返回】
        /// </summary>
        /// <param name="name">原始名称：机器码或用户名</param>
        /// <returns></returns>
        public string CreateName(string name)
        {
            if (m_isPC)
            {
                return CombineStrings(name, ToolCode);//目的是让使用者不会发现多个工具都是一个注册机器码
            }
            else
            {
                return name;
            }
        }
        /// <summary>
        /// 初始化注册信息：从配置文件中读取信息
        /// </summary>
        public virtual void Init()
        {
            //取出name、regCode
            try
            {
                lock (this)
                {
                    string tmp = File.ReadAllText(m_regFileName);
                    tmp = EncryptOP.DecryptString(tmp);
                    string[] strs = CommonOP.SplitString(tmp, CommonOP.Spliter);
                    if (strs.Length == 2)
                    {
                        m_name = strs[0];
                        m_regCode = strs[1];
                        //解析出时间、数量、反射信息
                        tmp = this.RemoveCRC(m_regCode);
                        string[] infos = tmp.Split(CommonOP.Spliter.ToCharArray());
                        m_date = DateTime.Parse(EncryptOP.DecryptString(infos[2]));
                        m_number = int.Parse(this.GetNumber(infos[3]));
                        m_reflectorInfo = EncryptOP.DecryptString(infos[4]);
                    }
                }
            }
            catch
            {
                this.ResetInfo();
            }
        }
        /// <summary>
        /// 将注册信息保存到配置文件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="regCode"></param>
        public virtual void Save(string name, string regCode)
        {
            lock (this)
            {
                string tmp = name + CommonOP.Spliter + regCode;
                tmp = EncryptOP.EncryptString(tmp);
                File.WriteAllText(m_regFileName, tmp);
            }
        }

        /// <summary>
        /// 初步验证注册码是否通过
        /// </summary>
        /// <returns></returns>
        public virtual bool CheckName()
        {
            //验证名称加密后与注册码中的一致性
            bool res = false;
            try
            {
                string tmp = this.RemoveCRC(m_regCode);
                string name = tmp.Split(CommonOP.Spliter.ToCharArray())[0];
                if (EncryptOP.EncryptString(m_name) == name)
                {
                    res = true;
                }
            }
            catch
            {
            }
            return res;
        }
        /// <summary>
        /// 验证当前的工具是否过期
        /// </summary>
        /// <param name="netValidate">是否需要网络验证</param>
        /// <returns></returns>
        public virtual bool CheckDate(bool netValidate)
        {
            bool res = false;
            try
            {
                //取本地时间验证
                //取时间服务器时间验证
                string tmp = this.RemoveCRC(m_regCode);
                DateTime date = DateTime.Parse(EncryptOP.DecryptString(tmp.Split(CommonOP.Spliter.ToCharArray())[2]));
                if (date > DateTime.Now)
                {
                    try
                    {
                        if (netValidate)
                        {
                            DateTime dt = CommonOP.GetChineseDateTime();
                            res = (dt.Year <= 1 || dt.AddDays(1) > DateTime.Now);
                        }
                        else
                        {
                            res = true;
                        }
                    }
                    catch { res = true; }
                }

            }
            catch
            {
            }
            return res;
        }
        /// <summary>
        /// 验证当前的工具代码是否正确
        /// </summary>
        /// <returns></returns>
        public virtual bool CheckToolCode()
        {
            bool res = false;
            try
            {
                string tmp = this.RemoveCRC(m_regCode);
                string toolCode = tmp.Split(CommonOP.Spliter.ToCharArray())[1];
                if (EncryptOP.EncryptString(m_toolCode) == toolCode)
                {
                    res = true;
                }
            }
            catch
            {
            }
            return res;
        }
        /// <summary>
        /// 验证当前的用户数是否合法
        /// </summary>
        /// <param name="count"></param>
        public virtual bool CheckNumber(int count)
        {
            bool res = false;
            try
            {
                string tmp = this.RemoveCRC(m_regCode);
                string num = this.GetNumber(tmp.Split(CommonOP.Spliter.ToCharArray())[3]);
                if (count.ToString() == num)
                {
                    res = true;
                }
            }
            catch
            {
            }
            return res;

        }

        /// <summary>
        /// 暗桩验证Name是否正确
        /// </summary>
        public virtual void VNM()
        {
            //验证Name
            //验证Name是否包含正确的ToolCode
            //单机：验证机器码是否一致
            try
            {
                if (m_isPC)
                {
                    string mc = GetMC();
                    if (mc != MC)
                    {
                        this.Close();
                    }
                    if (this.CreateName(mc) != Name)
                    {
                        this.Close();
                    }
                }
                string rc = this.RemoveCRC(EncryptOP.DecryptString(this.BakCode));
                string name = rc.Split(CommonOP.Spliter.ToCharArray())[0];
                if (EncryptOP.EncryptString(m_name) != name)
                {
                    this.Close();
                }
            }
            catch
            {
                this.Close();
            }

        }
        /// <summary>
        /// 暗桩验证时间
        /// </summary>
        public virtual void VDT()
        {
            //验证rc中的dt是否正确
            //比较本机的时间，是否过期
            //比较网络服务器，是否过期
            try
            {
                string rc = this.RemoveCRC(EncryptOP.DecryptString(this.BakCode));
                DateTime date = DateTime.Parse(EncryptOP.DecryptString(rc.Split(CommonOP.Spliter.ToCharArray())[2]));
                if (date < DateTime.Now)
                {
                    this.Close();
                }
                else
                {
                    try
                    {
                        if (CommonOP.GetChineseDateTime().AddDays(1) < DateTime.Now)
                        {
                            this.Close();
                        }
                    }
                    catch { }
                }
            }
            catch
            {
                this.Close();
            }
        }
        /// <summary>
        /// 暗桩验证账户数
        /// </summary>
        /// <param name="count"></param>
        public virtual void VNB(int count)
        {
            //验证rc中的nb是否正确
            //验证传入的数量是否正确
            try
            {
                string rc = this.RemoveCRC(EncryptOP.DecryptString(this.BakCode));
                string[] infos = rc.Split(CommonOP.Spliter.ToCharArray());
                int number = int.Parse(this.GetNumber(infos[3]));
                if (count > number)
                {
                    this.Close();
                }
            }
            catch
            {
                this.Close();
            }
        }
        /// <summary>
        /// 暗桩验证注册码
        /// </summary>
        public virtual void VRC()
        {
            try
            {
                string rc = EncryptOP.DecryptString(this.BakCode);
                string tmp = this.RemoveCRC(rc);
                string[] infos = tmp.Split(CommonOP.Spliter.ToCharArray());
                if (infos.Length != 5)
                {
                    this.Close();
                }
                if (RegCode != rc)
                {
                    this.Close();
                }
            }
            catch
            {
                this.Close();
            }
        }
        /// <summary>
        /// 暗桩验证工具码
        /// </summary>
        public virtual void VTC()
        {
            try
            {
                string rc = this.RemoveCRC(EncryptOP.DecryptString(this.BakCode));
                string[] infos = rc.Split(CommonOP.Spliter.ToCharArray());
                if (infos[1] != EncryptOP.EncryptString(ToolCode))
                {
                    this.Close();
                }
            }
            catch
            {
                this.Close();
            }
        }
        /// <summary>
        /// 暗桩验证CRC
        /// </summary>
        public virtual void VCRC()
        {
            try
            {
                string rc = EncryptOP.DecryptString(EncryptOP.DecryptString(this.BakCode));
                if (!this.CheckCRC(rc, true))
                {
                    this.Close();
                }
                rc = rc.Remove(0, 1);
                if (!this.CheckCRC(rc, false))
                {
                    this.Close();
                }
                rc = rc.Remove(rc.Length - 1);
                if (!this.CheckCRC(rc, true))
                {
                    this.Close();
                }
                rc = rc.Remove(0, 1);
                if (!this.CheckCRC(rc, false))
                {
                    this.Close();
                }
            }
            catch
            {
                this.Close();
            }
        }
        /// <summary>
        /// 随机抛出异常，必须在主线程中才能真正抛出来
        /// </summary>
        public virtual void ThrowRandom()
        {
            int r = m_random.Next(4);
            if (r == 0)
            {
                int t = 1 / int.Parse("0");
            }
            if (r == 1)
            {
                object o = null;
                Console.WriteLine(o.ToString());
            }
            if (r == 2)
            {
                int[] a = new int[1];
                Console.WriteLine(a[2]);
            }
            if (r >= 3)
            {
                System.Net.Dns.GetHostEntry("siji");
            }
        }


        /// <summary>
        /// 组成提交到服务器的信息字符串
        /// </summary>
        /// <param name="userName">帐户名</param>
        /// <param name="toolCode">软件产品代号</param>
        /// <param name="specialInfo">软件需要的额外信息，如网页游戏外挂需要知道额外的服务器url区分多个同名帐户</param>
        /// <param name="registerCode">客户拿到的注册码</param>
        /// <param name="version">客户使用的软件产品的版本</param>
        /// <returns>提交到服务器的客户端信息</returns>
        public string GetClientInfo(string userName, string toolCode, string specialInfo, string registerCode, string version)
        {
            string res = CommonOP.Spliter + userName
                + CommonOP.Spliter + toolCode
                + CommonOP.Spliter + specialInfo
                + CommonOP.Spliter + MC
                + CommonOP.Spliter + registerCode
                + CommonOP.Spliter + version;
            return Base64.Encrypt(res);
        }

        /// <summary>
        /// 创建试用帐户，返回注册码或抛异常
        /// </summary>
        /// <param name="clientInfo">提交到服务器的信息</param>
        /// <returns>注册码</returns>
        public string TrivialAccount(string clientInfo)
        {
            string res = "";
            try
            {
                res = EncryptOP.DecryptString(s_VR.TrialAccount(clientInfo));
            }
            catch
            {
                throw new Exception("netError");
            }
            if (res.StartsWith(SFL.CommonOP.Spliter))
            {
                return Base64.Decrypt(res.Substring(1));
            }
            else
            {
                throw new Exception(res);
            }
        }

        /// <summary>
        /// 网络验证使用：获取程序运行所需的信息
        /// </summary>
        /// <param name="clientInfo">提交到服务器的信息</param>
        /// <param name="enableMachine">是否激活本机</param>
        public void ValidateAccount(string clientInfo, bool enableMachine)
        {
            string res = "";
            try
            {
                res = EncryptOP.DecryptString(s_VR.ValidateAccount(clientInfo));
            }
            catch
            {
                throw new Exception("netError");
            }
            if (res.StartsWith(SFL.CommonOP.Spliter))
            {
                string s = Base64.Decrypt(res.Substring(1));
                string[] infos = s.Split(CommonOP.Spliter.ToCharArray());
                m_reflectorInfo = infos[2];
            }
            else
            {
                throw new Exception(res);
            }
        }

        /// <summary>
        /// 获取最新版下载地址
        /// </summary>
        /// <param name="clientInfo">提交到服务器的信息</param>
        /// <returns></returns>
        public string GetNewVersionUrl(string clientInfo)
        {
            string res = "";
            try
            {
                res = EncryptOP.DecryptString(s_VR.GetNewVersionUrl(clientInfo));
            }
            catch
            {
                throw new Exception("netError");
            }
            if (res.StartsWith(SFL.CommonOP.Spliter))
            {
                return Base64.Decrypt(res.Substring(1));
            }
            else
            {
                throw new Exception(res);
            }
        }

        /// <summary>
        /// 获取网络验证异常的消息表达
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public string GetNetExceptionMsg(Exception ex)
        {
            switch (ex.Message.ToLower())
            {
                case "expire":
                    return LanguageResource.GetInfo(LanguageResource.EX_Model_Register_ExpiredErr);
                case "fail":
                    return LanguageResource.GetInfo(LanguageResource.EX_Model_Register_FailErr);
                case "serverfail":
                    return LanguageResource.GetInfo(LanguageResource.EX_Model_Register_ServerFailErr);
                case "islatest":
                    return LanguageResource.GetInfo(LanguageResource.EX_Model_Register_IsLatestErr);
                case "occupy":
                    return LanguageResource.GetInfo(LanguageResource.EX_Model_Register_OccupyErr);
                case "neterror":
                    return LanguageResource.GetInfo(LanguageResource.EX_Model_Register_NetErr);
            }
            return "";
        }


        private void Test()
        {
            Console.WriteLine(CreateName(this.MC));
            Console.WriteLine(CommonOP.ReverseString("1234"));
            return;
            string ctt = System.IO.File.ReadAllText(@"f:\t.txt");
            string pat = @"\d{4}\w\d{1,2}\w\d{1,2}\w";
            //pat = @"document.write";
            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(pat);
            Console.WriteLine(r.Match(ctt).Value);
        }

        private void TestGetTime()
        {
            DateTime dt = CommonOP.GetChineseDateTime();
            Console.WriteLine(dt);
        }


        private void TestRC()
        {
            m_isPC = false;
            string rc = "NTkxY1JGSlBWRnByV214c2RGSnNPV3hXTUZ_eFYyNWtNaVJuZG5hRXJFMXFbM2xPUjFGNVdrZGJiaFl6YkhqRXJFMTZWVEZPYnpsVllucFdVRlY2UWpSVVYydDNbV3N4VW1SdVdYYUVyRmxVUlRKUFZtdDVWaGh_VGxKRlNuUlhWb1l5WkdkMmRzU3NXb2ROTlUweFZuUldhVFZVVm5wV2RGaFlbREprWjNbMlVGWXYv";
            rc = "9637OWIyZFBIYWRmM01UQmxbaUpoYTNsbGFtY3aErDFkMzhPVEE1TVdWMGEyOXPErDAxMDJOekF4WkRrNU9Ub3RNVEl0TXpFdsSsZDkzZFpqVTNZVE5oTVRBd05HSXaErDAzMTdOREJpWlZKaFowaHVbaTh2WEIv";
            m_toolCode = "etool";
            m_name = "baoyang";
            m_regCode = rc;
            this.Save(m_name, rc);
            //this.Init();
            //this.InitTimer();
            //this.Start();
            DateTime start = DateTime.Now;
            for (int i = 0; i < 1; i++)
            {
                Console.WriteLine(this.CheckToolCode());
                this.VNM();
                this.VCRC();
                this.VDT();
                this.VNB(10);
                this.VRC();
                this.VTC();
            }
            DateTime end = DateTime.Now;
            Console.WriteLine((end - start).TotalMilliseconds);

        }

        private void TestRandom()
        {
            int ttl = 0;
            for (int j = 0; j < 20; j++)
            {
                int cnt = 0;
                for (int i = 0; i < 100; i++)
                {
                    if (RandomAction(10))
                    {
                        cnt++;
                    }
                }
                ttl += cnt;
                Console.WriteLine(cnt);
            }
            Console.WriteLine("average：" + ttl / 20);
        }
    }
}
